package net.lahuobao.mall.controller.admin;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import net.lahuobao.mall.entity.admin.MallProductRecommendHistoryEntity;
import net.lahuobao.mall.service.admin.MallProductRecommendHistoryServiceI;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.jeecgframework.core.beanvalidator.BeanValidators;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.net.URI;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;

/**   
 * @Title: Controller
 * @Description: 推荐历史
 * @author zhangdaihao
 * @date 2016-09-16 14:01:00
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/mallProductRecommendHistoryController")
public class MallProductRecommendHistoryController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(MallProductRecommendHistoryController.class);

	@Autowired
	private MallProductRecommendHistoryServiceI mallProductRecommendHistoryService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private Validator validator;
	


	/**
	 * 推荐历史列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("net/lahuobao/mall/admin/mallProductRecommendHistoryList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(MallProductRecommendHistoryEntity mallProductRecommendHistory,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(MallProductRecommendHistoryEntity.class, dataGrid);
		//查询条件组装器
		if (StringUtil.isNotEmpty(mallProductRecommendHistory.getProductname())) {
			cq.like("productname", "%" + mallProductRecommendHistory.getProductname() + "%");
			mallProductRecommendHistory.setProductname(null);
		}

		if (StringUtil.isNotEmpty(mallProductRecommendHistory.getRecommendtype())) {
			cq.eq("recommendtype", mallProductRecommendHistory.getRecommendtype());
		}
		cq.addOrder("sort", SortDirection.asc);

		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, mallProductRecommendHistory, request.getParameterMap());
		this.mallProductRecommendHistoryService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除推荐历史
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(MallProductRecommendHistoryEntity mallProductRecommendHistory, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		mallProductRecommendHistory = systemService.getEntity(MallProductRecommendHistoryEntity.class, mallProductRecommendHistory.getId());
		message = "推荐历史删除成功";
		mallProductRecommendHistoryService.delete(mallProductRecommendHistory);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加推荐历史
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(MallProductRecommendHistoryEntity mallProductRecommendHistory, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(mallProductRecommendHistory.getId())) {
			message = "推荐历史更新成功";
			MallProductRecommendHistoryEntity t = mallProductRecommendHistoryService.get(MallProductRecommendHistoryEntity.class, mallProductRecommendHistory.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(mallProductRecommendHistory, t);
				mallProductRecommendHistoryService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "推荐历史更新失败";
			}
		} else {
			message = "推荐历史添加成功";
			mallProductRecommendHistoryService.save(mallProductRecommendHistory);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 推荐历史列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(MallProductRecommendHistoryEntity mallProductRecommendHistory, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(mallProductRecommendHistory.getId())) {
			mallProductRecommendHistory = mallProductRecommendHistoryService.getEntity(MallProductRecommendHistoryEntity.class, mallProductRecommendHistory.getId());
			req.setAttribute("mallProductRecommendHistoryPage", mallProductRecommendHistory);
		}
		return new ModelAndView("net/lahuobao/mall/admin/mallProductRecommendHistory");
	}
	
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<MallProductRecommendHistoryEntity> list() {
		List<MallProductRecommendHistoryEntity> listMallProductRecommendHistorys=mallProductRecommendHistoryService.getList(MallProductRecommendHistoryEntity.class);
		return listMallProductRecommendHistorys;
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<?> get(@PathVariable("id") String id) {
		MallProductRecommendHistoryEntity task = mallProductRecommendHistoryService.get(MallProductRecommendHistoryEntity.class, id);
		if (task == null) {
			return new ResponseEntity(HttpStatus.NOT_FOUND);
		}
		return new ResponseEntity(task, HttpStatus.OK);
	}

	@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResponseEntity<?> create(@RequestBody MallProductRecommendHistoryEntity mallProductRecommendHistory, UriComponentsBuilder uriBuilder) {
		//调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<MallProductRecommendHistoryEntity>> failures = validator.validate(mallProductRecommendHistory);
		if (!failures.isEmpty()) {
			return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
		}

		//保存
		mallProductRecommendHistoryService.save(mallProductRecommendHistory);

		//按照Restful风格约定，创建指向新任务的url, 也可以直接返回id或对象.
		String id = mallProductRecommendHistory.getId().toString();
		URI uri = uriBuilder.path("/rest/mallProductRecommendHistoryController/" + id).build().toUri();
		HttpHeaders headers = new HttpHeaders();
		headers.setLocation(uri);

		return new ResponseEntity(headers, HttpStatus.CREATED);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> update(@RequestBody MallProductRecommendHistoryEntity mallProductRecommendHistory) {
		//调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<MallProductRecommendHistoryEntity>> failures = validator.validate(mallProductRecommendHistory);
		if (!failures.isEmpty()) {
			return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
		}

		//保存
		mallProductRecommendHistoryService.saveOrUpdate(mallProductRecommendHistory);

		//按Restful约定，返回204状态码, 无内容. 也可以返回200状态码.
		return new ResponseEntity(HttpStatus.NO_CONTENT);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.NO_CONTENT)
	public void delete(@PathVariable("id") String id) {
		mallProductRecommendHistoryService.deleteEntityById(MallProductRecommendHistoryEntity.class, id);
	}


	@RequestMapping(params = "back")
	@ResponseBody
	public void back(Integer id, HttpServletResponse response) {
		AjaxJson json = new AjaxJson();
		try {
			mallProductRecommendHistoryService.back(id);
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMsg(e.getMessage());
			e.printStackTrace();
		} finally {
			renderData(response, json);
		}
	}

}
